package gui.cen.home;

import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javax.swing.*;
import javax.swing.border.TitledBorder;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class HomeScreen extends JPanel{
    private Ticker tick;
    public HomeScreen(){
        super(new BorderLayout());
        this.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        JPanel p1 = new JPanel(new BorderLayout());
        p1.setBorder(BorderFactory.createEmptyBorder(0,2,0,2));
        HTMLRenderer home = new HTMLRenderer();
        home.setFont(new Font("Tahoma", Font.PLAIN, 10));
        home.renderFrontEnd();
        p1.add(home,BorderLayout.CENTER);

        this.add(p1,BorderLayout.CENTER);

        JPanel p0 = new JPanel(new BorderLayout());
        p0.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Server News", TitledBorder.LEFT, TitledBorder.TOP, new Font("Tahoma", Font.PLAIN, 10)));

        String html =  this.getNews();
        tick = new Ticker(html);
        p0.add(tick,BorderLayout.CENTER);
        this.add(p0,BorderLayout.SOUTH);
        this.setVisible(true);
    }

    public void startTicker(){tick.setGoGo(true);tick.run();}

    public void stopTicker(){tick.setGoGo(false);}

    private String getNews(){
        String news = "";
        try{
            BufferedReader reader = read("http://www.bioinf.manchester.ac.uk/segminator/segminator_II/news");
            String input = "";
            while ((input = reader.readLine()) != null){news = news + input;}
            reader.close();
        }
        catch(Exception e){news = "Sorry! No news available";}
        return news;
    }
    private static BufferedReader read(String url) throws Exception{return new BufferedReader(new InputStreamReader(new URL(url).openStream()));}

}
